<?php
//status options
DEFINE("STATUS_LIVE", 1);
DEFINE("STATUS_PENDING", 2);
DEFINE("STATUS_ARCHIVED", 3);

class Form_render
{
	private $__Form_elements;
	public $Status_array = array(
		0 => array(0 => 1, 1 => "Live"),
		1 => array(0 => 2, 1 => "Pending"),
		2 => array(0 => 3, 1 => "Archive")
		);

	function __construct($form_elements)
	{
		$this->__Form_elements = $form_elements;
	}

	public function Render_form()
	{
        $onchange = "";
		$str = "";
		$elements_array_count = count($this->__Form_elements);
		//echo $elements_array_count;
		if($elements_array_count > 0)
		{
			foreach($this->__Form_elements as $elements)
			{
				switch ($elements['type'])
				{
					case "textbox":
						$str .= "
								<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
									<input type=\"text\" name=\"".$elements['name']."\" id=\"".$elements['name']."\" value=\"".$elements['value']."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\">\n
								</li>\n
							";
							break;
					case "password":
						$str .= "
								<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
									<input type=\"".$elements['type']."\" name=\"".$elements['name']."\" id=\"".$elements['name']."\" value=\"".$elements['value']."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\">\n
								</li>\n
							";
							break;
					case "textarea":
							$str .= "
								<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
									<textarea  name=\"".$elements['name']."\" id=\"".$elements['name']."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\">".$elements['value']."</textarea>\n
								</li>\n
							";
							break;
					case "textarea_small":
							$str .= "
								<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
									<textarea  name=\"".$elements['name']."\" id=\"".$elements['name']."\" class=\"".$elements['class']."\" style=\"height: 75px; ".$elements['style']."\">".$elements['value']."</textarea>\n
								</li>\n
							";
							break;
					case "wysiwyg":
							$oFCKeditor = new FCKeditor($elements['name']) ;
							$oFCKeditor->BasePath = FCK_EDITOR_URL ;
							$oFCKeditor->Value = stripslashes($elements['value']);
							$oFCKeditor->Width = isset($elements['wysiwyg_width']) ? $elements['wysiwyg_width'] : '310' ;
							$oFCKeditor->Height = isset($elements['wysiwyg_height']) ? $elements['wysiwyg_height'] : '300' ;
							$html_editor = $oFCKeditor->Create();

							$str .= "
								<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
									".$html_editor."\n
								</li>\n
							";
							break;
					case "file":
						$str .= "
								<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
									<input type=\"".$elements['type']."\" name=\"".$elements['name']."\" id=\"".$elements['name']."\" value=\"".$elements['value']."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\">\n
								</li>\n
							";

							if(strlen($elements['value']) > 0)
							{
								if(strpos($elements['value'], "jpg") > 0 || strpos($elements['value'], "gif") > 0 || strpos($elements['value'], "png") > 0)
								{
									$image_width = 300;
									@$file_size = getimagesize($_SERVER['DOCUMENT_ROOT'].$elements['data'].$elements['value']);
									if($file_size[0] > 300)
									{
										$image_width = 300;
									}
									else{
										$image_width = $file_size[0];
									}
								
									$str .= "
										<li>\n
											<label for=\"\">".$elements['title']." Preview:</label>\n
											<img src=\"".$elements['data'].$elements['value']."\" id=\"image_preview_".$elements['name']."\" width=\"".$image_width."\"><br /><div id=\"delete_".$elements['name']."_div\"><a href=\"javascript:;\" id=\"delete_".$elements['name']."_link\" name=\"original_".$elements['name']."\">delete image</a></div>\n
										</li>\n
									";
								}
								else
								{
									$str .= "
										<li>
											<label for=\"\">".$elements['title']." Preview:</label>\n
											<a href=\"".$elements['data'].$elements['value']."\" class=\"orange\" id=\"file_preview\" target=\"_blank\">Download file &gt;&gt; ".$elements['value']."</a><br /><div id=\"delete_file_div\"><a href=\"javascript:;\" id=\"delete_file\" class=\"white\">delete file</a></div>\n
										</li>\n
									";
								}
							}
							break;
					case "selectlist":
							//check the data element of the array as that is where the drop downlist data will be
							$data_array_count = count($elements['data']);
							//echo $data_array_count;
							$opt = "";

								//create the option list
								for($d = 0; $d < $data_array_count; $d++)
								{
									$opt .= "<option value=\"".$elements['data'][$d][0]."\"";
										if($elements['value'] == $elements['data'][$d][0])
										{
											$opt .= " selected";
										}
									$opt .=">".$elements['data'][$d][1]."</option>\n";
								}

							$str .= "
									<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
										<select name=\"".$elements['name']."\" id=\"".$elements['name']."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\" ".$onchange.">\n".$opt."\n</select>\n
									</li>\n
									";
							break;
					case "status_selectlist":
							//check the data element of the array as that is where the drop downlist data will be
							$status_array_count = count($this->Status_array);
							//echo $data_array_count;
							$opt = "";

								//create the option list
								for($d = 0; $d < $status_array_count; $d++)
								{
									$opt .= "<option value=\"".$this->Status_array[$d][0]."\"";
										if($elements['value'] == $this->Status_array[$d][0])
										{
											$opt .= " selected";
										}
									$opt .=">".$this->Status_array[$d][1]."</option>\n";
								}

							$str .= "
									<li>\n
									<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
										<select name=\"".$elements['name']."\" id=\"".$elements['name']."\" ".$onchange.">\n".$opt."\n</select>\n
									</li>\n
									";
							break;
					case "checkbox":

							//check the data element of the array as that is where the drop downlist data will be
							$data_array_count = count($elements['data']);
							//echo $data_array_count;
							$opt = "";

								//create the option list
								for($d = 0; $d < $data_array_count; $d++)
								{
									if($elements['value'] == $elements['data'][$d][0])
										{
											$checked .= " checked ";
										}
									$opt .= "<input type=\"checkbox\" name=\"".$elements['name']."\" id=\"".$elements['name']."\" value=\"".$elements['data'][$d][0]."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\" ".$checked.">".$elements['data'][$d][1]."<br />\n";
									$checked = "";
								}

							$str .= "
									<li>
										<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
										".$opt."\n
									</li>\n";
							break;
					case "radiobuttons":
							//check the data element of the array as that is where the drop downlist data will be
							$data_array_count = count($elements['data']);
							//echo $data_array_count;
							$opt = "";

								//create the option list
								for($d = 0; $d < $data_array_count; $d++)
								{
									if($elements['value'] == $elements['data'][$d][0])
										{
											$checked .= " checked ";
										}
									$opt .= "<input type=\"radio\" name=\"".$elements['name']."\" id=\"".$elements['name']."\" value=\"".$elements['data'][$d][0]."\" class=\"".$elements['class']."\" style=\"".$elements['style']."\" ".$checked.">".$elements['data'][$d][1]."<br />\n";

									$checked = "";
								}

							$str .= "
									<li>
										<label for=\"".$elements['name']."\">".$elements['title']."</label>\n
										".$opt."\n
									</li>
							";

							break;
					default:
							break;
				}
			}
		}
		return $str;
	}

	static function Get_status_name($status)
	{
		switch ($status)
		{
			case STATUS_LIVE:
				return "Live";
				break;
			case STATUS_PENDING:
				return "Pending";
				break;
			case STATUS_ARCHIVE:
				return "Archive";
				break;
		}
	}
	
}
?>
